package pl.edu.romanow.snippet

import net.liftweb.http.DispatchSnippet
import xml.NodeSeq
import net.liftweb.util.Helpers._
import net.liftweb.http.js.JsCmds.Script
import pl.edu.romanow.model.{Database, GraphVisualisationUtil, Facebook}
import net.liftweb.json.JsonAST.JArray
import net.liftweb.json.{Printer, JsonAST}
import pl.edu.romanow.util.AppException
import net.liftweb.http.js.{JsCmd, JsCmds}

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 18.04.12
 * Time: 17:32
 * To change this template use File | Settings | File Templates.
 */

class GraphSnippet extends DispatchSnippet {

  override def dispatch = {
    case "graphData" if !Facebook.currentUser.is.isEmpty => graphData _
    case _ => in: NodeSeq => in
  }


  def graphData(in: NodeSeq): NodeSeq = {
    ("#data *" #> GraphSnipped.drawGraph(
      GraphVisualisationUtil.displayFriends(Facebook.currentFacebookNode
        .openOr(throw AppException("No logged user!")))) &
      "#graph_scripts" #> Script(GraphVisualisationUtil.nodeToolTipFunction()
        & GraphVisualisationUtil.onNodeClickFunction())
      )(in)
  }


}

object GraphSnipped {

  def drawGraph(json: String): NodeSeq = Script(JsCmds.Run("var json = %s".format(json)));

  def reDrawGraph(newData: String): JsCmd = {
    JsCmds.Run("json = " + newData + ";") &
      JsCmds.Run("drawGraph('your_graph');")
  }

}




